package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qxzn.app.bean.MedicineCaseRecordBean;
import com.qxzn.app.bean.MedicineRemindBean;
import com.qxzn.app.bean.RepeatDateBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.medicalhealth.activity.AddRepeatDateActivity;
import com.qxzn.app.medicalhealth.activity.FamilyMedicalRecordsActivity;
import com.qxzn.app.medicalhealth.activity.TakeMedicalRemindActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FamilyMedicalRecordsContentAdapter extends PagerAdapter {

    private String page;
    private Context mContext;
    private List dataList;

    public FamilyMedicalRecordsContentAdapter(Context context, String page) {
        this.mContext = context;
        this.page = page;
    }

    public void setData(List dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (this.dataList == null) {
            return 0;
        }
        return this.dataList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View page = getView(position);
        container.addView(page);
        return page;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View) {
            container.removeView((View) object);
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public View getView(final int position) {
        View view = null;
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_sub_family_cases, null);
            holder = new ViewHolder(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (FamilyMedicalRecordsActivity.class.getSimpleName().equals(page)) {
            MedicalRecordsAdapter adapter = new MedicalRecordsAdapter(mContext, (List<MedicineCaseRecordBean.DataBean>)dataList.get(position));
            holder.rvCases.setAdapter(adapter);
        } else if (TakeMedicalRemindActivity.class.getSimpleName().equals(page)) {
//            holder.ivIcon.setImageResource(R.mipmap.leak_canary_icon);
            holder.tvCases.setText("已设定的提醒");
            TakeMedicalRemindAdapter adapter = new TakeMedicalRemindAdapter(mContext, (List<MedicineRemindBean.DataBean.ResultBean>)dataList.get(position));
            holder.rvCases.setAdapter(adapter);
        } else if (AddRepeatDateActivity.class.getSimpleName().equals(page)) {
            holder.llHead.setVisibility(View.GONE);
            RepeatDateBean repeatDateBean = ((List<RepeatDateBean>)dataList).get(position);
            RepeatDateAdapter adapter = new RepeatDateAdapter(mContext, repeatDateBean.getData(), repeatDateBean.getType());
            holder.rvCases.setAdapter(adapter);
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R2.id.iv_icon)
        ImageView ivIcon;
        @BindView(R2.id.tv_cases)
        TextView tvCases;
        @BindView(R2.id.rv_cases)
        RecyclerView rvCases;
        @BindView(R2.id.ll_head)
        LinearLayout llHead;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            rvCases.setLayoutManager(new LinearLayoutManager(view.getContext()));
        }
    }
}
